/**
 * Created by lumix on 2015/4/22.
 */
var express = require('express');
var router = express.Router();
var Promise = require('promise');
var logger = require('log4js').getLogger('common');
var commonUtils = require('./../../utils/commonUtils');
var sensitiveService = require('../service/SensitiveService');
var config = require('../../../config')
var multiparty = require('multiparty');
var util = require('util');
var fs = require('fs');

router.post('/upload',function(req,res,next){
    var form = new multiparty.Form();
    form.encoding = 'utf-8';
    form.uploadDir = config.main.fileUploadPath;
    form.parse(req, function(err, fields, files) {
        if(err){
            res.json({'success':false});
        }
        //console.log(files.upload[0].path);
        var suffix =/\.[^\.]+/.exec(files.upload[0].path);
        console.log(suffix)
        var fileName = commonUtils.getFileName(config.main.fileUploadPath,suffix[0],0);
        fs.rename(files.upload[0].path,fileName,function(err){
            if(err){
                res.json({'success':false});
            }else{
                res.json({'success':true,'url':fileName});
            }
        })

    });

})

router.get('/toUpload',function(req,res,next){

    res.render('shoot/views/upload');
})


//校验敏感词
router.post('/checkSensitive',function(req,res,next){
    var content = req.body.content;
    sensitiveService.check(content).then(function(result){
        if(result){
            res.json({'success':true});
        }else{
            res.json({'success':false});
        }
    },function(err){
        console.log(err);
        res.json({'success':false});
    })
})

//获取敏感词

router.post('/uploadSensitive',function(req,res,next){

    sensitiveService.uploadSensitive().then(function(sens){
        var data = {};
        data.sensitives = sens;
        res.json({'success':true,'data':data});
    },function(err){
        console.log(err);
        res.json({'success':false});
    })
})


module.exports = router;
